package com.future.mayi.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.app.App;
import com.future.mayi.dialog.CommonDialog;
import com.future.mayi.dialog.KefuDialog;
import com.future.mayi.model.GoodsItemModel;
import com.future.mayi.model.RefundInfoModel;
import com.future.mayi.response.RefundInfoResponse;
import com.future.mayi.utils.PriceUtils;
import com.future.mayi.widget.ListImgView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RefundInfoActivity extends BaseTitleActivity {
    @BindView(R.id.tv_cause)
    TextView tvCause;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_info)
    TextView tvInfo;
    @BindView(R.id.list_img)
    ListImgView listImg;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_refund_state)
    TextView tvRefundState;
    @BindView(R.id.tv_refund_content)
    TextView tvRefundContent;
    @BindView(R.id.tv_enter)
    TextView tvEnter;
    @BindView(R.id.iv_img_state)
    ImageView ivImgState;
    @BindView(R.id.tv_num)
    TextView tvNum;


    private String orderId;
    private RefundInfoModel model;
    private GoodsItemModel goodsItemModel;


    public static void startActivity(Context context, String orderId, GoodsItemModel model) {
        Intent intent = new Intent(context, RefundInfoActivity.class);
        intent.putExtra("orderId", orderId);
        intent.putExtra("goodsItemModel", model);
        context.startActivity(intent);
    }

    @Override
    protected void initTopBar() {
        initBack();
        getTitleText().setText("退款详情");

    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_refund_info);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initDates() {
        orderId = getIntent().getStringExtra("orderId");
        goodsItemModel = (GoodsItemModel) getIntent().getSerializableExtra("goodsItemModel");
        getData();
    }

    private void getData() {
        new HttpRequest<RefundInfoResponse>()
                .with(this).setApiCode(ApiCst.refundInfoOrder)
                .addParam("token", App.getToken(mContext))
                .addParam("return_id", goodsItemModel.getReturn_id())
                .setListener(new HttpRequest.OnNetworkListener<RefundInfoResponse>() {
                    @Override
                    public void onSuccess(RefundInfoResponse response) {
                        model = response.model;
                        setView();
                    }

                    @Override
                    public void onFail(String message, RefundInfoResponse response) {
                        toast(message);
                    }
                }).start(new RefundInfoResponse());
    }

    private void setView() {
        if (model == null) return;
        setCause(model.getReturn_desc());

        setInfo(model.getReturn_explain());
        setTime();
        setListImg(model.getReturn_img());
        setText(tvNum, String.valueOf(model.getTotal_return_num()));
        if ((model.getReturn_status() == 40)) {
            setPrice(model.getActual_return_price());
        } else setPrice(model.getReturn_price());


        //退货状态:10=待审核,20=商家同意,30=商家拒绝(已关闭),40=已退款,50=已取消
        if (model.getReturn_status() == 10 || model.getReturn_status() == 30 || model.getReturn_status() == 50) {
            tvEnter.setVisibility(View.VISIBLE);
            tvEnter.setText((model.getReturn_status() == 30 || model.getReturn_status() == 50) ? "重新申请退款" : "取消退款");
        } else tvEnter.setVisibility(View.GONE);


        if (model.getReturn_status() == 10) {
            tvRefundState.setText("退款审核中");
            ivImgState.setImageResource(R.drawable.dingdan_tuikuan_on);
            tvRefundContent.setText("平台将会及时和您联系退款事宜");
        } else if (model.getReturn_status() == 30) {
            tvRefundState.setText("退款被拒绝");
            ivImgState.setImageResource(R.drawable.dingdan_tuikuan_fail);
//            tvRefundContent.setText(TextUtils.isEmpty(model.getSeller_refuse_desc()) ? "平台拒绝了您的退款申请，请重新申请" : model.getSeller_refuse_desc());
            tvRefundContent.setText("平台拒绝了您的退款申请，请联系售后客服");
        } else if (model.getReturn_status() == 20) {
            tvRefundState.setText("退款成功");
            ivImgState.setImageResource(R.drawable.dingdan_tuikuan_success);
//            tvRefundContent.setText(TextUtils.isEmpty(model.getSeller_desc()) ? "退款金额已经原路返回到您的支付账户" : model.getSeller_desc());
            tvRefundContent.setText("退款金额已经原路返回到您的支付账户");
        } else if (model.getReturn_status() == 40) {
            tvRefundState.setText("退款成功");
            ivImgState.setImageResource(R.drawable.dingdan_tuikuan_success);
//            tvRefundContent.setText(TextUtils.isEmpty(model.getSeller_desc()) ? "退款金额已经原路返回到您的支付账户" : model.getSeller_desc());
            tvRefundContent.setText("退款金额已经原路返回到您的支付账户");
        } else if (model.getReturn_status() == 50) {
            tvRefundState.setText("退款已取消");
            ivImgState.setImageResource(R.drawable.dingdan_tuikuan_fail);
            tvRefundContent.setText("订单退款已取消");
        }

    }

    @Override
    protected void initListener() {


    }


    public void setCause(String cause) {
        setText(tvCause, "退款原因    " + cause);
    }

    public void setPrice(double price) {
        setText(tvPrice, "退款金额    ¥" + PriceUtils.getPriceText2(price));
    }

    public void setTime() {
        if (TextUtils.isEmpty(model.getUpdatetime())) {
            tvTime.setVisibility(View.GONE);
            findViewById(R.id.view_line_time).setVisibility(View.GONE);
            return;
        }
        //退货状态:10=待审核,20=商家同意,30=商家拒绝(已关闭),40=已退款,50=已取消
        if (model.getReturn_status() == 30) {
            tvTime.setVisibility(View.VISIBLE);
            findViewById(R.id.view_line_time).setVisibility(View.VISIBLE);
            tvTime.setText("拒绝时间    " + model.getUpdatetime());
        } else if (model.getReturn_status() == 20 || model.getReturn_status() == 40) {
            tvTime.setVisibility(View.VISIBLE);
            findViewById(R.id.view_line_time).setVisibility(View.VISIBLE);
            tvTime.setText("退款时间    " + model.getUpdatetime());
        }else {
            tvTime.setVisibility(View.GONE);
            findViewById(R.id.view_line_time).setVisibility(View.GONE);
        }

    }

    public void setInfo(String info) {
        setText(tvInfo, "退款说明    " + (info == null ? "" : info));
    }

    public void setListImg(List<String> list) {
        listImg.addImg(list);
    }


    public void cancelRefund() {

        showLoadingDialog();
        new HttpRequest<DefaultResponse>()
                .with(this).setApiCode(ApiCst.cancelRefund)
                .addParam("token", App.getToken(mContext))
                .addParam("return_id", model.getReturn_id())
                .setListener(new HttpRequest.OnNetworkListener<DefaultResponse>() {
                    @Override
                    public void onSuccess(DefaultResponse response) {
                        dissmissLoadingDialog();
                        toast(response.message);
                        finish();
                    }

                    @Override
                    public void onFail(String message, DefaultResponse response) {
                        dissmissLoadingDialog();
                        tvEnter.setEnabled(true);
                        toast(message);
                    }
                }).start(new DefaultResponse());

    }


    @OnClick({R.id.ll_server, R.id.tv_enter})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_server:
                new KefuDialog(mContext).show(1);
                break;
            case R.id.tv_enter: {
                if (model == null) return;
                tvEnter.setEnabled(false);
                if (model.getReturn_status() == 10) {
                    //撤销退款
                    new CommonDialog.Builder(this)
                            .setCancelable(false)
                            .setMessage("确认取消本次退款申请吗？")
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                    tvEnter.setEnabled(true);
                                }
                            })
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                    cancelRefund();
                                }
                            }).show();
                } else if (model.getReturn_status() == 30 || model.getReturn_status() == 50) {
                    ApplyRefundActivity.startActivity(this, orderId, goodsItemModel);
                    finish();
                } else tvEnter.setEnabled(true);
            }
            break;
        }
    }
}
